package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gulimall.product.entity.BrandEntity;
import com.gulimall.product.service.BrandService;
import com.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;

import com.gulimall.product.dao.CategoryBrandRelationDao;
import com.gulimall.product.entity.CategoryBrandRelationEntity;
import com.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存品牌与其关联的分类
     * @param categoryBrandRelation 品牌与分类关联关系实体
     */
    @Override
    public void saveAll(CategoryBrandRelationEntity categoryBrandRelation) {
        //获取品牌名称
        Long brandId = categoryBrandRelation.getBrandId();
        String brandName = brandService.getById(brandId).getName();
        //获取分类名称
        Long catelogId = categoryBrandRelation.getCatelogId();
        String categoryName = categoryService.getById(catelogId).getName();
        //设置品牌与分类的关联关系
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);

        this.save(categoryBrandRelation);
    }

    /**
     * 关联更新品牌数据
     * @param brandId 待更新的品牌id
     * @param name 更新内容
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        //UPDATE pms_category_brand_relation SET brand_name=name WHERE (brand_id = brandId)
        this.update(Wrappers.lambdaUpdate(CategoryBrandRelationEntity.class)
                .eq(CategoryBrandRelationEntity::getBrandId,brandId)
                .set(CategoryBrandRelationEntity::getBrandName,name)
        );
    }


    /**
     * 关联更新分类数据
     * @param categoryId 待更新的品牌id
     * @param name 更新内容
     */
    @Override
    public void updateCategory(Long categoryId, String name) {
        //UPDATE pms_category_brand_relation SET brand_name=name WHERE (brand_id = brandId)
        this.update(Wrappers.lambdaUpdate(CategoryBrandRelationEntity.class)
                .eq(CategoryBrandRelationEntity::getCatelogId,categoryId)
                .set(CategoryBrandRelationEntity::getCatelogName,name)
        );
    }

    /**
     * 根据分类id获取其关联的品牌信息
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandByCatId(Long catId) {

        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = Wrappers.lambdaQuery();
        if (catId!= null){
            queryWrapper.eq(CategoryBrandRelationEntity::getCatelogId,catId);
        }

        List<CategoryBrandRelationEntity> list = this.list(queryWrapper);

        List<Long> brandIds = list.stream().map(item -> {
            return item.getBrandId();
        }).collect(Collectors.toList());

        return brandService.listByIds(brandIds);
    }
}